﻿
using Dapper;
using HBase;
using hCommand;
using hconnect; 
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Controller
{
    public class CarrierSynchro1 : AbsCarrierSynchro<CarrierSignalStatus>
    {
        protected CarrierConnect1 connect = null;

        protected override AbsStatus parse(AbsStatus old)
        {
            Dictionary<int, CarrierSignalStatus> carrierMessage = new Dictionary<int, CarrierSignalStatus>();
            var res = new CarrierStatus();
            res.c = carrierMessage;
            byte[] data = connect.reader(14, 0, 144);
            if (data == null )
            {
                return null;
            }
            commmonStnIn(carrierMessage, data, 0, ConstStn.stn1);
            commmonStnOut(carrierMessage, data, 20, ConstStn.Crane1Out1Stn);
            commmonStnIn(carrierMessage, data, 40, ConstStn.stn3);
            commmonStnOut(carrierMessage, data, 60, ConstStn.Crane1Out2Stn);

            commmonStnIn(carrierMessage, data, 80, ConstStn.stn5);
            commmonStnOut(carrierMessage, data, 100, ConstStn.stn6);
            commmonStnIn(carrierMessage, data, 120, ConstStn.stn7);
            commmonStnOut(carrierMessage, data, 140, ConstStn.stn8);
            return res;
        }

        void commmonStnIn(Dictionary<int, CarrierSignalStatus> carrierMessage,
            byte[] data, int begin, int id)
        {
            int no = id;
            int height = data[++begin];
            
            int scanArrive = data[++begin];
            begin += 2;
            float weight = S7ByteUtil.toFloat(data, begin);
            begin += 4;
            int arrive = data[begin];
            int huiArrive = data[++begin];
            int run = data[++begin];
            CarrierSignalStatus signal = new CarrierSignalStatus(no, height, weight, scanArrive, arrive, huiArrive,run);
            carrierMessage.Add(signal.p, signal);
        }
        void commmonStnOut(Dictionary<int, CarrierSignalStatus> carrierMessage,
           byte[] data, int begin, int id)
        {
            int no = id;
            int isExistMaterial = data[++begin];
            int isRun = data[++begin];
            int huiArrive = data[++begin];
            CarrierSignalStatus signal = new CarrierSignalStatus(no, isExistMaterial, isRun, huiArrive);
            carrierMessage.Add(signal.p, signal);
        }
    }
}
